$(function() {
	validateRule();
	//打开图标列表
    $("#ico-btn").click(function(){
        layer.open({
            type: 2,
			title:'图标列表',
            content: path.format("/static/FontIcoList.html"),
            area: ['480px', '90%'],
            success: function(layero, index){
                //var body = layer.getChildFrame('.ico-list', index);
                //console.log(layero, index);
            }
        });
    });
});
$.validator.setDefaults({
	submitHandler : function() {
		submit();
	}
});
function submit() {
	$.ajax({
		type : "POST",
		url : path.format("/auth/menu/addMenu"),
		data : $('#menuForm').serialize(),
		error : function(request) {
			layer.msg("Connection error");
		},
		success : function(r) {
		    var data = JSON.parse(r);
			if (data.status == 1000) {
				parent.layer.msg("保存成功",function () {

                });
				if($("#parentId").val()==0){
				    loadMenuName();
                }
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				layer.alert(data.desc);
			}
		}
	});
}
function loadMenuName() {
    //移除select
    parent.jQuery("#menuName").empty();
    $.ajax({
        url: path.format("/auth/menu/hasChildMenu"),
        type: "post",
        data: {},
        success: function (r) {
            var data = JSON.parse(r);
            if (data.status == 1000) {
                var arr = new StringBulider();
                arr.append("<option")
                    .append(" value=")
                    .append()
                    .append(">")
                    .append("直接或搜索选择菜单")
                    .append("</option>");
                for (var i=0;i<data.model.length;i++){
                    arr.append("<option")
                        .append(" value=")
                        .append(data.model[i].id)
                        .append(">")
                        .append(data.model[i].name)
                        .append("</option>");
                }
                parent.currentMenu = 0;
                parent.jQuery("#menuName").append(arr.toString());
                parent.resetSelect();
            } else {
                layer.msg(data.desc,function () {

                });
            }
        }
    });
}


function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#menuForm").validate({
		rules : {
			name : {
				required : true
			},
			type : {
				required : true
			},
            permsCode:{
			    required : true
            },sort:{
			    max:127
            }
		},
		messages : {
			name : {
				required : icon + "请输入菜单名"
			},
			type : {
				required : icon + "请选择菜单类型"
			},
            permsCode : {
				required : icon + "请输入权限标识"
			},
            sort : {
				max : icon + "最大不能超过127"
			}
		}
	});
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.iframeAuto(index);
}